<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form
        ref="formRef"
        class="mb-[-16px]"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item label="服务名称" prop="goodsId">
          <!-- <el-input class="w-56" v-model="queryParams.goodsId" /> -->
          <el-select
            v-model="queryParams.goodsId"
            filterable
            placeholder="请选择服务名称"
          >
            <el-option
              v-for="item in serviceList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格名称" prop="name">
          <el-input class="w-56" v-model="queryParams.name" />
        </el-form-item>
        <!-- <el-form-item label="价格，表示该规格的价格" prop="price">
          <el-input class="w-56" v-model="queryParams.price" />
        </el-form-item>
        <el-form-item
          label="数量递增价格，如果为空表示不递增"
          prop="incrementPrice"
        >
          <el-input class="w-56" v-model="queryParams.incrementPrice" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input class="w-56" v-model="queryParams.createdBy" />
        </el-form-item>
        <el-form-item label="修改人" prop="updatedBy">
          <el-input class="w-56" v-model="queryParams.updatedBy" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input class="w-56" v-model="queryParams.remark" />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <el-button
          v-perms="['specification:add']"
          type="primary"
          @click="handleAdd()"
        >
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新增
        </el-button>
      </div>
      <el-table
        class="mt-4"
        size="large"
        v-loading="pager.loading"
        :data="pager.lists"
      >
        <el-table-column label="服务名称" prop="goodsName" min-width="100" />
        <el-table-column label="规格名称" prop="name" min-width="100" />
        <el-table-column label="价格" prop="price" min-width="100" />
        <el-table-column
          label="超出价格（每台）"
          prop="incrementPrice"
          min-width="100"
        />
        <el-table-column label="创建时间" prop="createTime" min-width="100" />
        <el-table-column label="更新时间" prop="updateTime" min-width="100" />
        <!-- <el-table-column label="创建人" prop="createdBy" min-width="100" />
        <el-table-column label="修改人" prop="updatedBy" min-width="100" /> -->
        <el-table-column label="备注" prop="remark" min-width="100" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
              v-perms="['specification:edit']"
              type="primary"
              link
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              v-perms="['specification:del']"
              type="danger"
              link
              @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
  </div>
  <edit-popup
    v-if="showEdit"
    ref="editRef"
    @success="getLists"
    @close="showEdit = false"
    :serviceList="serviceList"
  />
</template>
<script lang="ts" setup>
import { specificationDelete, specificationLists } from "@/api/specification";
import { usePaging } from "@/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";

import { apiServiceLists } from "@/api/service/lists";
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false);
const queryParams = reactive({
  goodsId: "",
  name: "",
  price: "",
  incrementPrice: "",
  createdBy: "",
  updatedBy: "",
  remark: "",
});
const serviceList = reactive([]);

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: specificationLists,
  params: queryParams,
});

const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.getDetail(data);
};

// const serviceList: any[] = [];
getserviceList(1);
function getserviceList(page: number) {
  apiServiceLists({ pagenoNo: page, pageSize: 60 }).then((res) => {
    console.log(res, "res---");
    res.lists.forEach((element: any) => {
      serviceList.push(element);
    });
    console.log(serviceList, "serviceList");
    if (res.count > page * 60) {
      getserviceList(page + 1);
    }
  });
}

const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await specificationDelete({ id });
  feedback.msgSuccess("删除成功");
  getLists();
};

getLists();
</script>
